﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using MVCPets.Data;
using MVCPets.Extensions;
using MVCPets.Data.Model;
using DataAccess.Core.Attributes;

namespace MVCPets.Models
{
	[BypassValidation]
  public class PetsModel
  {
    #region Public Members
    public SelectList Statuses { get; set; }
    public string ID { get; set; }
    public string Age { get; set; }
    public string Breed { get; set; }
    public string Color { get; set; }
    public string Gender { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Fee { get; set; }
    public DateTime? Listed { get; set; }
    public DateTime? Edited { get; set; }
    public int Status { get; set; }
    [DisplayName("Status")]
    public string StatusName { get; set; }
    public string ThumbnailType { get; set; }
    public byte[] ThumbnailData { get; set; }

    public string LinkText
    {
      get
      {
        return "{0} {1} {2} {3}".FormatString(this.Name, this.Gender, this.Color, this.Breed)
          .Replace(' ', '-')
          .Replace('/', '-');
      }
    }
    #endregion

    #region Constructors
    public PetsModel()
    {
      GetStatusList();
    }

    public PetsModel(Guid id)
    {
			Pet foo = Data.DataAccess.DataStore.LoadObject<Pet>(id.ToString());
			
      if (foo != null)
			{
				this.Age = foo.Age;
				this.Breed = foo.Breed;
				this.Color = foo.Color;
				this.ID = foo.ID;
				this.Fee = foo.Fee;
				this.Description = foo.Description;
				this.Edited = foo.Edited;
				this.Gender = foo.Gender;
				this.Listed = foo.Listed;
				this.Name = foo.Name;
				this.Status = foo.Status;
				this.StatusName = Data.DataAccess.DataStore.LoadObject<PetStatus>(foo.Status).StatusName;
				GetStatusList(foo.ID);
			}   
    }
    #endregion

    #region Static Methods
    public static PetsListModel GetPetsByStatus(string status)
    {
      var ret = new PetsListModel { Status = PetStatusModel.GetStatus(status) };
			ret.Pets = (from i in Data.DataAccess.DataStore.Query<Pet>()
                  join x in Data.DataAccess.DataStore.Query<PetStatus>() on i.Status equals x.ID
                  where x.StatusName == status
                  select new PetsModel
								  	{
											Age = i.Age,
											Fee = i.Fee,
											Breed = i.Breed,
											Color = i.Color,
											ID = i.ID,
											Description = i.Description,
											Edited = i.Edited,
											Gender = i.Gender,
											Listed = i.Listed,
											Name = i.Name,
											Status = i.Status,
											StatusName = x.StatusName
	 								  }).ToList();
      
      return ret;
    }

    public static PetImage GetThumbnail(Guid ID)
    {			
      PetImage ret = null;
			var foo = Data.DataAccess.DataStore.LoadObject<Pet>(ID.ToString(), true);

        if (foo != null)
        {
          ret = new PetImage
          {
            ContentType = foo.ThumbnailType,
            ImageData = foo.ThumbnailData
          };
        }

      return ret;
    }
    #endregion

    #region Public Methods
    public bool Save()
    {
      var ret = false;

      if (!string.IsNullOrEmpty(this.ID)) ret = Update();
      else ret = Insert();

      return ret;
    }

    public bool Delete()
    {
			return Data.DataAccess.DataStore.DeleteObject<Pet>(this.ID);
    }
    #endregion

    #region Private Methods
    private bool Insert()
    {
			var pet = new Pet
			{
				Age = this.Age,
				Breed = this.Breed,
				Color = this.Color,
				Description = this.Description,
				Fee = this.Fee,
				Gender = this.Gender,
				Listed = DateTime.Now,
				Name = this.Name,
				Status = this.Status,
				Edited = DateTime.Now,
				ID = Guid.NewGuid().ToString()
			};

			if (this.ThumbnailData != null)
			{
				pet.ThumbnailType = this.ThumbnailType;
				pet.ThumbnailData = this.ThumbnailData;
			}
			return Data.DataAccess.DataStore.InsertObject(pet);
    }

    private bool Update()
    {
			Pet p = new Pet();
			p.ID = this.ID;
			p.Age = this.Age;
			p.Breed = this.Breed;
			p.Color = this.Color;
			p.Description = this.Description;
			p.Fee = this.Fee;
			p.Gender = this.Gender;
			p.Name = this.Name;
			p.Status = this.Status;
			p.Edited = DateTime.Now;
			return Data.DataAccess.DataStore.UpdateObject(p);
    }

    private void GetStatusList()
    {
			Statuses = new SelectList(Data.DataAccess.DataStore.LoadEntireTable<PetStatus>().OrderBy(r => r.StatusName).ToList(), "id", "StatusName");
    }

    private void GetStatusList(string id)
    {
			Statuses = new SelectList(Data.DataAccess.DataStore.LoadEntireTable<PetStatus>().OrderBy(r => r.StatusName).ToList(), "id", "StatusName", id);
    }
    #endregion

		public static bool AddImage(string petID, string contentType, byte[] fBytes)
		{
			Image i = new Image();
			i.ID = Guid.NewGuid().ToString();
			i.ImageData = fBytes;
			i.Pet = petID;
			i.ContentType = contentType;

			return Data.DataAccess.DataStore.InsertObject(i);
		}

		public static List<Image> GetImageData(string PetID)
		{
			return Data.DataAccess.DataStore.Query<Image>().Where(R => R.Pet == PetID).ToList();
		}

		public static Image GetImage(Guid gd)
		{
			return Data.DataAccess.DataStore.LoadObject<Image>(gd.ToString(), true);
		}
	}
}